<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible="visible"
    width="600px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <!-- <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="dataForm.nickName"
          placeholder="请输入用户昵称"
        ></el-input>
      </el-form-item> -->
      <el-form-item label="用户头像" prop="headerUrl">
         <ImageUploadSingle v-model="dataForm.headerUrl"></ImageUploadSingle>
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="dataForm.realName"
          placeholder="请输入真实姓名"
        ></el-input>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" :rows="4" v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { getObj, putObj } from "@/api/shop_user/user";
import ImageUploadSingle from '@/components/imageUploadSingle';

export default {
  props:{
    visible: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      canSubmit: false,
      dataForm: {
        navType:'1',
        headerUrl:'',
        status:'1'
      },
      dataRule: {
        
      }
    };
  },
  components: {
    ImageUploadSingle,
  },
  watch:{
  },
  methods: {
    init(id) {
      this.canSubmit = true;
      if(id){
        getObj(id).then((response) => {
          this.dataForm = response.data.data;
        });
      }
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          let data = {
            id: this.dataForm.id,
            nickName: this.dataForm.nickName,
            realName: this.dataForm.realName,
            remark: this.dataForm.remark,
            headerUrl: this.dataForm.headerUrl
          }

          if (this.dataForm.id) {
            putObj(data)
              .then((data) => {
                this.$notify.success("修改成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
    //重置表单
    closeDialog() {
      this.$emit('update:visible', false)
    },
  },
};
</script>
